import { _decorator, Animation, AudioSource, Component, Node, warn } from 'cc';
import { Globals } from './Globals';
import StorageManager from './Data/StorageManager';
import { ResourcesUtil } from './ResourcesUtil';
import { Enum_AudioClip } from './Enum';
const { ccclass, property } = _decorator;

@ccclass('Eletric')
export class Eletric extends Component {

    // 伤害
    hurt: number = 35;

    private _audioSound: AudioSource;
    protected onLoad() {
        this._audioSound = this.getComponent(AudioSource);
        ResourcesUtil.loadAudioClip(Enum_AudioClip.EM_LIGHT_LAUNCH_01).then((clip) => {
            this._audioSound.clip = clip
        })

    }
    protected async onEnable(): Promise<void> {
        // 音效
        this.node.getComponent(Animation).on(Animation.EventType.LASTFRAME, this.onFinished, this);
        //播放音效
        try {
            if (!this._audioSound || !this._audioSound.clip) {
                console.log("@@@ loadAudioClip err: ", Enum_AudioClip.EM_LIGHT_LAUNCH_01)
                return;
            }
            StorageManager.instance.sound && this._audioSound.playOneShot(this._audioSound.clip);
        } catch (err) { warn(err) }

    }
    protected onDisable(): void {
        this.node.getComponent(Animation).off(Animation.EventType.LASTFRAME, this.onFinished, this);

    }

    start() {

    }

    update(deltaTime: number) {

    }

    onFinished() {
        console.log("销毁");

        Globals.putNode(this.node);
    }
}


